package chat.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.whu.zj.MyApplication;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;


/**
 * 网络相关数据
 * Created by 凯 on 2015/10/15.
 */
public class NetWorkUtil {

    private static String UUID = null;

    /**
     * 获取设备的mac地址
     *
     * @return
     */
    private static String getMAC() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);


            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }


    /**
     * 获取唯一标识手机
     *
     * @return
     */
    public static String getUUID() {
        if (UUID == null) {
//            UUID = "d404+" + getMAC() + getMyUUID();
            UUID = "d404+" + Installation.getID(MyApplication.share());
        }

        return UUID;
//        return UUID;
    }

    // 网络是否已经连接
    public static boolean haveNetworkConnection(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork == null) return false;
        return activeNetwork.isConnected();
    }


    // 获取设备唯一标示
    public static String getMyUUID(){
        final TelephonyManager tm = (TelephonyManager) MyApplication.share().getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        String tmDevice = "", tmSerial = "", tmPhone = "", androidId = "";
        if (tm != null)
        {
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
        }

        androidId = "" + android.provider.Settings.Secure.getString(MyApplication.share().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
        String deviceUuid = androidId.hashCode()+"-"+(((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
        return deviceUuid;
    }


}
